package com.campus.customer.controller;

import com.campus.common.bean.common.Resp;
import com.campus.customer.vo.UserInfoVo;
import com.campus.customer.bean.address.AddressBean;
import com.campus.customer.entity.Address;
import com.campus.customer.service.AddressService;
import com.campus.customer.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private SessionService sessionService;
    @Autowired
    private AddressService addressService;


    /**
     * 获取用户收货地址列表
     * @return 收货地址列表
     */
    @GetMapping("/list")
    public Resp<List<Address>> getUserAddressList() {
        // 获取当前用户信息
        UserInfoVo userInfo = sessionService.getUserInfo();

        // 查询用户的收货地址列表
        List<Address> addresses = addressService.getUserAddressList(userInfo.getId());

        return Resp.success(addresses);
    }

    /**
     * 获取用户收货地址详情
     * @param id 地址ID
     * @return 地址详情
     */
    @GetMapping("/detail")
    public Resp<Address> getAddressDetail(@RequestParam Long id) {
        // 查询地址详情
        Address address = addressService.getById(id);

        return Resp.success(address);
    }

    /**
     * 添加用户收货地址
     * @param addressBean 收货地址对象
     * @return 操作结果
     */
    @PostMapping("/save")
    public Resp<String> saveUserAddress(@RequestBody AddressBean addressBean) {
        // 获取当前用户信息
        UserInfoVo userInfo = sessionService.getUserInfo();

        // 设置地址的用户ID
        addressBean.setUserId(userInfo.getId());

        // 添加用户的收货地址
        addressService.saveUserAddress(addressBean);

        return Resp.success("地址添加成功");
    }
    
    /**
     * 删除用户收货地址
     * @param id 地址ID
     * @return 操作结果
     */
    @PostMapping("/delete")
    public Resp<String> deleteUserAddress(@RequestParam Long id) {
        // 删除用户的收货地址
        addressService.removeById(id);
        
        return Resp.success("地址删除成功");
    }

}
